<?php
/**
* Update logs
* May 31, 2011 - trungdt - First create
*/
class rsnewsitem {
    function form($object,$config, $loc) {
        $i18n = exponent_lang_loadFile('datatypes/newsitem.php');

        global $user,$db;

        if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
        exponent_forms_initialize();

        $form = new form();
        if (!isset($object->id)) {
            $object->title = '';
            $object->internal_name = '';
            $object->body = '';
            $object->publish = null;
            $object->unpublish = null;
            $object->background_id = 0;
            $object->file_id = 0;
            $object->summary="";
            $object->tags="";
            $object->province_id="";
            $object->collection_id=-1;
            $object->show_small__image_in_detail=1;
            $object->featured=0;
        } else {
            $form->meta('id',$object->id);
            if ($object->publish == 0) $object->publish = null;
            if ($object->unpublish == 0) $object->unpublish = null;
        }

        //$form->register(null,'',new htmlcontrol('<br /><div class="moduletitle">'.$i18n['news_content'].'</div><hr size="1" />'));
        $form->register('title',$i18n['title'],new textcontrol($object->title));
        $form->register('summary',"Summary of news",new texteditorcontrol($object->summary, 5, 80));
        $form->register('body',$i18n['body'],new htmleditorcontrol($object->body));
        // Oct 22, 2010 - trung - remove background_id - begin
        //$form->register('background_id','Large image on Feature tab', new ajax_select_file('background_id',0,$object->background_id,true));
        // Oct 22, 2010 - trung - remove background_id - end
        $form->register('file_id','News cover Image', new ajax_select_file("file_id",$config->image_group,$object->file_id,true));
        $form->register('show_small','Show small image in detail', new checkboxcontrol($object->show_small__image_in_detail,true));
        $form->register('featured_check','Featured News - Hot news', new checkboxcontrol($object->featured,true));
        /**
        * Oct 28, 2010 - immanuel192 - add ajax_image_list control - begin
        */
        //$form->register("images_list","Images List", new ajax_image_list("image_list",$config->image_group,$object->id,true));
        /**
        * Oct 28, 2010 - immanuel192 - add ajax_image_list control - end
        */
        $form->register(null,'',new htmlcontrol('<br /><div class="moduletitle">'.$i18n['publish_information'].'</div><hr size="1" />'));
        //$form->register('publish',$i18n['publish'],new popupdatetimecontrol($object->publish,$i18n['nopublish']));
        $checked = empty($object->publish) ? true : false;
        $form->register('publish',$i18n['publish'],new yuidatetimecontrol($object->publish,$i18n['nopublish'], true, true, false, $checked));
        //$form->register('unpublish',$i18n['unpublish'],new popupdatetimecontrol($object->unpublish,$i18n['nounpublish']));
        $checked = empty($object->unpublish) ? true : false;
        $form->register('unpublish',$i18n['unpublish'],new yuidatetimecontrol($object->unpublish,$i18n['nounpublish'], true, true, false, $checked));
        
        $form->register('tag_header','',new htmlcontrol('<br /><div class="moduletitle">'.$i18n['tags'].'</div><hr size="1" />'));
        $form->register('tags',"Enter tags for this news, delimiter by comma",new texteditorcontrol($object->tags, 5, 80));
        
        // News Collections
        $form->register('collections_header','',new htmlcontrol('<br /><div class="moduletitle">Collections</div><hr size="1" />'));
        $temp=clone $loc;
        $temp->int=''; // fix bug that set int property to empty in order to search all collections in this news module
        $collections=$db->selectDropdown('rsnews_collections','name',"location_data = '".serialize($temp)."'");
        $first_col=-2;
        foreach ($collections as $k=>$v)
        {
            if ($first_col == -2)
            {
                $first_col=$k;
            }
            if (($object->collection_id ==-1) 
                || ($object->collection_id == $k))
            {
                break;
            }
        }
        if ($object->collection_id == -1)
        {
            $object->collection_id =$first_col;
        }
        $form->register('col_id','Collection Group',new dropdowncontrol($object->collection_id,$collections,false));
        // end of news collections
        
        // Location
        $form->register('location_header','',new htmlcontrol('<br /><div class="moduletitle">Location info</div><hr size="1" />'));
        $provinces=$db->selectDropdown('provinces','name');
        $default_province = 0;
        foreach ($provinces as $mvalue=>$mcaption)
        {
            if ($mvalue == $object->province_id)
            {
                $default_province=$mvalue;
                break;
            }
        }
        $object->province_id=$default_province;
        $form->register('provinceid','Province',new dropdowncontrol($object->province_id,$provinces,true));
        // end of location
        
        $form->register('submit','',new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));

        return $form;
    }
    
    function update($values,$object) {
        if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
        exponent_forms_initialize();
        $object->title = $values['tags'];
        $object->title = $values['title'];
        $object->summary = $values['summary'];
        $internal_name=$tinter=_to_vietnamese_with_nomark(_ab_safe_html_string($values['title']));
        global $db;
        do {
            $temp=$db->selectObject("newsitem","internal_name = '{$tinter}'");
            if ($temp!=null)
            {// we can use this internal name
                $tinter=$internal_name.rand(rand(0,100),9999);
            }
            else
            {
                $internal_name=$tinter;
                break;   
            }
        }while (0<1);
        $object->internal_name=$internal_name;
        $object->body = $values['body'];
        $object->file_id = intval($values['file_id']);
        $object->show_small__image_in_detail=(isset($values['show_small'])?1:0);
        $object->featured=(isset($values['featured_check'])?1:0);
        $object->province_id = intval($values['provinceid']);
        $object->collection_id = intval($values['col_id']);
        //$object->background_id = intval($values['background_id']);
        $object->publish = yuidatetimecontrol::parseData('publish',$values);
        $object->unpublish = yuidatetimecontrol::parseData('unpublish',$values);
        // for tag saving
        $object->tags = _ab_safe_html_string(trim($values['tags']));
        $object->tags = explode(",",$object->tags);
        foreach ($object->tags  as $k=>$v)
        {
            
        }
        // 
        return $object;
    }
    
    /**
    * Build up form object for real estate collections
    * 
    * @return form
    */
    function collections_form($object, $config=null) {
        if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
        exponent_forms_initialize();
        
        global $db;
        
        $form = new form();
            
        
        if (!isset($object->id)) {
            $object->name = '';
            $object->shortname='';
            $object->rank=0;
        } else {
            $form->meta('id',$object->id);
        }
        
        $form->validationScript="";
        $form->register('name','Name',new textcontrol($object->name,50,false,200));
        $form->register('shortname','Shortname',new textcontrol($object->shortname,50,false,200));
        $form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
        return $form;
    }
    
    /**
    * This function update all info in real estate collections
    * @author: trungdt
    */
    function collections_update($values,$object) {
        $object->name = _ab_safe_html_string($values['name']);
        $object->shortname = _ab_safe_html_string($values['shortname']);
        
        return $object;
    }
    
}

?>
